﻿using UnityEngine;

public class InputManager : MonoBehaviour 
{	
	private enum CONTROL_STATE
	{
		CS_ON,
		CS_OFF,
	}
	private CONTROL_STATE bulletTimeState = CONTROL_STATE.CS_OFF;
	private CONTROL_STATE manualBallSpawnState = CONTROL_STATE.CS_OFF;
	
	private enum PIE_CONTROL_STATE
	{
		CS_CW,
		CS_CCW,
		CS_IDLE,
	}
	PIE_CONTROL_STATE pieControlState = PIE_CONTROL_STATE.CS_IDLE;
	
	private PieController pieController;
	private BallSpawner ballSpawner;
	private BallList ballList;
	
	private bool waitForReleaseTripleTap = false;
	
	void Start() 
	{
		pieController = (PieController) GetComponent("PieController");
		ballSpawner = (BallSpawner) GetComponent("BallSpawner");
		ballList = (BallList) GetComponent("BallList");
	}
	
	void Update() 
	{
#if UNITY_ANDROID
		HandleAndroidInput();
#else
		HandlePcInput();
#endif		
		ProcessInputStates();
		ResetInputStates();
	}
	
	private void ProcessInputStates()
	{
		switch (pieControlState)
		{
		case PIE_CONTROL_STATE.CS_CW:
			pieController.RotatePie(PieController.DIRECTION.CW);
			break;
		case PIE_CONTROL_STATE.CS_CCW:
			pieController.RotatePie(PieController.DIRECTION.CCW);
			break;
		}
		
		switch (bulletTimeState)
		{
		case CONTROL_STATE.CS_ON:
			ballList.ballSpeedSlow = true;
			break;
			
		case CONTROL_STATE.CS_OFF:
			ballList.ballSpeedSlow = false;
			break;
		}
		
		switch (manualBallSpawnState)
		{
		case CONTROL_STATE.CS_ON:
			ballSpawner.SpawnBall();
			break;
			
		case CONTROL_STATE.CS_OFF:
			break;
		}		
	}
	
	private void ResetInputStates()
	{
		pieControlState = PIE_CONTROL_STATE.CS_IDLE;
		bulletTimeState = CONTROL_STATE.CS_OFF;
		manualBallSpawnState = CONTROL_STATE.CS_OFF;		
	}
	
	private void HandleAndroidInput() 
	{
		int touchCount = Input.touchCount;
		
		// Hack: prevents spamming tripletaps
		if (touchCount != 3 && waitForReleaseTripleTap)
		{
			waitForReleaseTripleTap = false;
		}
		
		switch (touchCount)
		{		
		case 1:
			pieControlState = Input.GetTouch(0).position.x > Screen.width/2 ?
				PIE_CONTROL_STATE.CS_CW : PIE_CONTROL_STATE.CS_CCW;			
			break;
			
		case 2:
			bulletTimeState = CONTROL_STATE.CS_ON;
			break;
			
		case 3:
			if (!waitForReleaseTripleTap)
			{
				manualBallSpawnState = CONTROL_STATE.CS_ON;
				waitForReleaseTripleTap = true;
			}
			break;
			
		default:
			break;
		}
	}
	
	private void HandlePcInput() 
	{
		float axisHoriz = Input.GetAxisRaw("Horizontal");
		
		if (axisHoriz != 0.0f)
		{
			pieControlState = axisHoriz > 0.0f ?
				PIE_CONTROL_STATE.CS_CW : PIE_CONTROL_STATE.CS_CCW;
		}
		
		if (Input.GetKey(KeyCode.LeftShift))
		{			
			bulletTimeState = CONTROL_STATE.CS_ON;
		}
		
		if (Input.GetKeyUp(KeyCode.Space))
		{
			manualBallSpawnState = CONTROL_STATE.CS_ON;
		}
		
		if (Input.GetKeyDown(KeyCode.Escape))
		{
			Application.Quit();
		}
	}
	
	public bool InputNextLevel()
	{
		bool retVal = false;
		
#if UNITY_ANDROID
		if (Input.touchCount == 2)
		{
			retVal = true;
		}
#else
		if (Input.GetKeyUp(KeyCode.Space))
		{
			retVal = true;
		}
#endif
		
		return retVal;
	}
}
